package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import bean.general.Preference;


@ManagedBean(name="daoGeneral")
@ApplicationScoped
public class DaoGeneral extends Dao {
	/**
	 * Get all the Preferences from database
	 * @param styleName
	 * @return
	 * @throws DaoException 
	 * @throws NotFoundException 
	 */
	public HashMap<String, Preference> getPreferences() throws DaoException, NotFoundException {
		HashMap<String, Preference> preferences = new HashMap<String, Preference>();
		ResultSet rs = null;
		
		String query = 
				"SELECT * " +
						"FROM " + Preference.TABLE_NAME
				;
		try {
			rs = st.executeQuery(query);
			log.log(3, "Execution: " + query);
		} catch (SQLException e) {
			throw new DaoException(this.getClass() + ".Error executing query: " + query);
		}

		try {
			int colNum = rs.getMetaData().getColumnCount();

			while (rs.next()) {
				HashMap<String, String> line = new HashMap <String,String>();
				for (int i = 1; i <= colNum; i++)
				{
					line.put(rs.getMetaData().getColumnLabel(i), rs.getString(i));
				}

				preferences.put(line.get(Preference.ID_COL), new Preference(line));
			}
			if (preferences.isEmpty())
				throw new NotFoundException(this.getClass() + ". No preferences found");

		} catch (SQLException e) {
			throw new DaoException(this.getClass() + ". Unable to get preferences from Database");
		}
		
		return preferences;
	}
}
